<?php

namespace App\Console\Commands\Guanyuan;

use App\Library\FeiShu\SendFs;
use App\Service\OpenShop\Statistical\SignOrderStatisticalService;
use Illuminate\Console\Command;
use Kuafu\GeneralFs\BuildFs;

class FriedChickenLegNotice extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'open_shop:fried_chicken_leg {env=prod}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '生炸鸡腿数据推送';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     * 文档 https://onepiece-group.feishu.cn/wiki/Lh93wofFHiVqz5kIclbc4LSbnwd
     * @return int
     * @throws \Exception
     */
    public function handle()
    {
        $date = date('Y-m-d', strtotime('-1 day'));

        $data = SignOrderStatisticalService::getFriedChickenLegData($date);

        if ($data['totalData']) {

            $card = [
                'elements' => [
                    [
                        'tag'     => 'markdown',
                        'content' => "**生炸鸡腿：{$date}**\n"
                    ],

                ]
            ];

            $card['elements'] = array_merge(
                $card['elements'], [
                    [
                        'tag'     => 'markdown',
                        'content' => '**订货数据**'
                    ],
                    [
                        'tag'              => 'column_set',
                        'flex_mode'        => 'none',
                        'background_style' => 'grey',
                        'columns'          => [
                            [
                                'tag'            => 'column',
                                'width'          => 'weighted',
                                'weight'         => 1,
                                'vertical_align' => 'top',
                                'elements'       => [
                                    [
                                        'tag'        => 'markdown',
                                        'content'    => ' ',
                                        'text_align' => 'center'
                                    ]
                                ]
                            ],
                            [
                                'tag'            => 'column',
                                'width'          => 'weighted',
                                'weight'         => 1,
                                'vertical_align' => 'top',
                                'elements'       => [
                                    [
                                        'tag'        => 'markdown',
                                        'content'    => '**MAU**',
                                        'text_align' => 'center'
                                    ]
                                ]
                            ],
                            [
                                'tag'            => 'column',
                                'width'          => 'weighted',
                                'weight'         => 1,
                                'vertical_align' => 'top',
                                'elements'       => [
                                    [
                                        'tag'        => 'markdown',
                                        'content'    => '**订货门店**',
                                        'text_align' => 'center'
                                    ]
                                ]
                            ],
                            [
                                'tag'            => 'column',
                                'width'          => 'weighted',
                                'weight'         => 1,
                                'vertical_align' => 'top',
                                'elements'       => [
                                    [
                                        'tag'        => 'markdown',
                                        'content'    => '**渗透率**',
                                        'text_align' => 'center'
                                    ]
                                ]
                            ],
                        ]
                    ],
                ]
            );
            foreach ($data['totalData'] as $totalData) {
                $card['elements'] = array_merge(
                    $card['elements'], [
                        [
                            'tag'              => 'column_set',
                            'flex_mode'        => 'none',
                            'background_style' => 'default',
                            'columns'          => [
                                [
                                    'tag'            => 'column',
                                    'width'          => 'weighted',
                                    'weight'         => 1,
                                    'vertical_align' => 'top',
                                    'elements'       => [
                                        [
                                            'tag'        => 'markdown',
                                            'content'    => "{$totalData['province']}",
                                            'text_align' => 'center'
                                        ]
                                    ]
                                ],
                                [
                                    'tag'            => 'column',
                                    'width'          => 'weighted',
                                    'weight'         => 1,
                                    'vertical_align' => 'top',
                                    'elements'       => [
                                        [
                                            'tag'        => 'markdown',
                                            'content'    => "{$totalData['shopNum']}",
                                            'text_align' => 'center'
                                        ]
                                    ]
                                ],
                                [
                                    'tag'            => 'column',
                                    'width'          => 'weighted',
                                    'weight'         => 1,
                                    'vertical_align' => 'top',
                                    'elements'       => [
                                        [
                                            'tag'        => 'markdown',
                                            'content'    => "{$totalData['orderStoreTotalNum']}",
                                            'text_align' => 'center'
                                        ]
                                    ]
                                ],
                                [
                                    'tag'            => 'column',
                                    'width'          => 'weighted',
                                    'weight'         => 1,
                                    'vertical_align' => 'top',
                                    'elements'       => [
                                        [
                                            'tag'        => 'markdown',
                                            'content'    => "{$totalData['shopPenetrationRate']}",
                                            'text_align' => 'center'
                                        ]
                                    ]
                                ],
                            ]
                        ],
                        [
                            'tag' => 'hr'
                        ],
                    ]
                );
            }

            $card['elements'] = array_merge(
                $card['elements'], [
                    [
                        'tag'     => 'markdown',
                        'content' => '**售卖数据**'
                    ],
                    [
                        'tag'              => 'column_set',
                        'flex_mode'        => 'none',
                        'background_style' => 'grey',
                        'columns'          => [
                            [
                                'tag'            => 'column',
                                'width'          => 'weighted',
                                'weight'         => 1,
                                'vertical_align' => 'top',
                                'elements'       => [
                                    [
                                        'tag'        => 'markdown',
                                        'content'    => ' ',
                                        'text_align' => 'center'
                                    ]
                                ]
                            ],
                            [
                                'tag'            => 'column',
                                'width'          => 'weighted',
                                'weight'         => 1,
                                'vertical_align' => 'top',
                                'elements'       => [
                                    [
                                        'tag'        => 'markdown',
                                        'content'    => '**堂食实收(k)**',
                                        'text_align' => 'center'
                                    ]
                                ]
                            ],
                            [
                                'tag'            => 'column',
                                'width'          => 'weighted',
                                'weight'         => 1,
                                'vertical_align' => 'top',
                                'elements'       => [
                                    [
                                        'tag'        => 'markdown',
                                        'content'    => '**堂食销量(k)**',
                                        'text_align' => 'center'
                                    ]
                                ]
                            ],
                            [
                                'tag'            => 'column',
                                'width'          => 'weighted',
                                'weight'         => 1,
                                'vertical_align' => 'top',
                                'elements'       => [
                                    [
                                        'tag'        => 'markdown',
                                        'content'    => '**外卖实收(k)**',
                                        'text_align' => 'center'
                                    ]
                                ]
                            ],
                            [
                                'tag'            => 'column',
                                'width'          => 'weighted',
                                'weight'         => 1,
                                'vertical_align' => 'top',
                                'elements'       => [
                                    [
                                        'tag'        => 'markdown',
                                        'content'    => '**外卖销量(k)**',
                                        'text_align' => 'center'
                                    ]
                                ]
                            ],
                            [
                                'tag'            => 'column',
                                'width'          => 'weighted',
                                'weight'         => 1,
                                'vertical_align' => 'top',
                                'elements'       => [
                                    [
                                        'tag'        => 'markdown',
                                        'content'    => '**外卖占比**',
                                        'text_align' => 'center'
                                    ]
                                ]
                            ],
                        ]
                    ],
                ]
            );
            foreach ($data['shopData'] as $totalData) {
                $card['elements'] = array_merge(
                    $card['elements'], [
                        [
                            'tag'              => 'column_set',
                            'flex_mode'        => 'none',
                            'background_style' => 'default',
                            'columns'          => [
                                [
                                    'tag'            => 'column',
                                    'width'          => 'weighted',
                                    'weight'         => 1,
                                    'vertical_align' => 'top',
                                    'elements'       => [
                                        [
                                            'tag'        => 'markdown',
                                            'content'    => "{$totalData['province']}",
                                            'text_align' => 'center'
                                        ]
                                    ]
                                ],
                                [
                                    'tag'            => 'column',
                                    'width'          => 'weighted',
                                    'weight'         => 1,
                                    'vertical_align' => 'top',
                                    'elements'       => [
                                        [
                                            'tag'        => 'markdown',
                                            'content'    => "{$totalData['actualIncome']}",
                                            'text_align' => 'center'
                                        ]
                                    ]
                                ],
                                [
                                    'tag'            => 'column',
                                    'width'          => 'weighted',
                                    'weight'         => 1,
                                    'vertical_align' => 'top',
                                    'elements'       => [
                                        [
                                            'tag'        => 'markdown',
                                            'content'    => "{$totalData['salesVolume']}",
                                            'text_align' => 'center'
                                        ]
                                    ]
                                ],
                                [
                                    'tag'            => 'column',
                                    'width'          => 'weighted',
                                    'weight'         => 1,
                                    'vertical_align' => 'top',
                                    'elements'       => [
                                        [
                                            'tag'        => 'markdown',
                                            'content'    => "{$totalData['takeawayActualIncome']}",
                                            'text_align' => 'center'
                                        ]
                                    ]
                                ],
                                [
                                    'tag'            => 'column',
                                    'width'          => 'weighted',
                                    'weight'         => 1,
                                    'vertical_align' => 'top',
                                    'elements'       => [
                                        [
                                            'tag'        => 'markdown',
                                            'content'    => "{$totalData['takeawaySalesVolume']}",
                                            'text_align' => 'center'
                                        ]
                                    ]
                                ],
                                [
                                    'tag'            => 'column',
                                    'width'          => 'weighted',
                                    'weight'         => 1,
                                    'vertical_align' => 'top',
                                    'elements'       => [
                                        [
                                            'tag'        => 'markdown',
                                            'content'    => "{$totalData['takeawayRate']}",
                                            'text_align' => 'center'
                                        ]
                                    ]
                                ],
                            ]
                        ],
                        [
                            'tag' => 'hr'
                        ],
                    ]
                );
            }

            // 飞书机器人推送
            BuildFs::newRobot(SendFs::ROBOT_FRIED_CHICKEN_LEG, BuildFs::MSG_TYPE_CARD)
                ->setCardContent($card)
                ->send();
        }

        logInfo('生炸鸡腿数据推送', collect([]), true, true);
    }

}



